Shader "HolaShader/PhongFragShader"
{
    //高光公式：specular= pow (max (0, (dot (reflectDir , viewDir))), _Gloss)
    //光线反射方向：reflectDic = reflect(normal, lightDir)或reflectDir = lightDir - 2 * dot (normal, lightDir) * normal
    //最终公式：color = ambient + diffuse + specular，环境+漫反射+高光
    Properties
    {
       _Diffuse("Diffuse color" , Color) = (1,1,1,1)
       _Specular("Specular color" , Color) = (1,1,1,1)
       _Gloss("Gloss", Range(8.0, 255)) = 0.2
    }
    SubShader
    {
        Tags { "LightMode" = "ForwardBase" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
            };

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldNormal = mul((float3x3)unity_ObjectToWorld, v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //环境光
                fixed3 ambient = normalize(UNITY_LIGHTMODEL_AMBIENT.xyz);
                //计算漫反射
                fixed3 worldNormalDir = normalize(i.worldNormal);
                fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormalDir, lightDir));
                //计算高光
                fixed3 reflectDir = normalize(reflect(-lightDir, worldNormalDir));
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
                fixed4 col = fixed4(ambient + diffuse + specular, 1);
                return col;
            }
            ENDCG
        }
    }
    FallBack "Specular"
}
